---
layout: overview
title: Examples
item: examples
---
      <main class="c-content">
        <section class="c-section">
          <div class="c-container -padded">
            <h1 class="c-heading -h2">Examples</h1>
            <p>StrongLoop provides complete example apps for all LoopBack's major features, including connecting to various kinds of data sources, creating model relations, adding access control, using all the SDKs, and so on.  For a complete list, see <a href="https://loopback.io/doc/en/lb3/Tutorials-and-examples.html">Tutorials and examples.</a></p>
          </div>

        <section class="c-section x-greylight">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Create backend APIs through models</h2>
            <div class="c-grid">
              <div class="_col -col-1-3">
                <p>Use the LoopBack generator to create the project and two models for office supplies app.</p>
              </div>
              <div class="_col -col-2-3">
                <pre class="cli"><span class="prompt">$</span> lb app office-supplies
<span class="prompt">?</span> What's the name of your application? <span class="input">(office-supplies)</span>
<span class="prompt">?</span> Enter name of the directory to contain the project: <span class="input">(office-supplies)</span>

<span class="prompt">?</span> Which version of LoopBack would you like to use? 3.x (current)
<span class="prompt">?</span> What kind of application do you have in mind? <span class="input">empty-server (An empty LoopBack API,
  without any configured models or datasources)</span>
...
<span class="prompt">$</span> lb model product
<span class="prompt">$</span> lb model category</pre>
              </div>
            </div>
          </div>
        </section>
        <section class="c-section">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Create a secure REST API</h2>
            <div class="c-grid">
              <div class="_col -col-1-3">
                <p>Secure all the model APIs, then allow admins to modify products.</p>
              </div>
              <div class="_col -col-2-3">
                <pre class="cli"><span class="prompt">$</span> lb acl</pre>
              </div>
            </div>
          </div>
        </section>
        <section class="c-section x-greylight">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Easily persist data</h2>
            <div class="c-grid">
              <div class="_col -col-1-3">
                <p>LoopBack models have a full set of methods for create, read, update, and delete (CRUD) operations. For example:</p>
              </div>
              <div class="_col -col-2-3">
                <pre><code class="language-javascript">category.create({name: 'stationery', id: 1});
product.create({
  categoryId: 1,
  name: 'Pencil',
  price: 0.99
});</code></pre>
              </div>
            </div>
          </div>
        </section>
        <section class="c-section">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Define model relations</h2>
            <div class="c-grid">
              <div class="_col -col-1-1">
                <p>Use simple JSON declarative syntax to set up relations between models.
                For example, the following specifies that a product belongs to one category
                (identified by categoryId) and has one owner (identified by ownerId).</p>
                <pre class="cli"><span class="prompt">$</span> lb relation
<span class="prompt">[?]</span> Select the model to create the relationship from: product
<span class="prompt">[?]</span> Relation type: belongs to
<span class="prompt">[?]</span> Choose a model to create a relationship with: category
<span class="prompt">[?]</span> Enter the property name for the relation: category
<span class="prompt">[?]</span> Optionally enter a custom foreign key: categoryId</pre>
              </div>
              <div class="_col -col-1-2">
                <p>This updates the <b>product.json</b> file as follows:</p>
                <pre style="max-height: 358px;"><code class="language-javascript">{
  "name": "product",
  "base": "PersistedModel",
  "properties": {
    "name": {
      "type": "string"
    },
    "category": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {
    "category": {
      "type": "belongsTo",
      "model": "category",
      "foreignKey": "categoryId"
    },
    "owner": {
      "type": "belongsTo",
      "model": "owner",
      "foreignKey": "ownerId"
    }
  },
  "acls": [],
  "methods": []
}</code></pre>
          </div>
            </div> <!-- /grid -->

          </div>
        </section>
        <section class="c-section x-greylight">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Run ad-hoc queries</h2>
            <p>Call directly into the LoopBack APIs directly from iOS, Android, or client JavaScript.</p>
            <div class="c-grid">
              <div class="_col -col-1-2">
                <h3 class="c-heading -h4">Node.js</h3>
                <pre><code class="language-javascript">MyProduct.find({
    where: {price: {lt: 100}},
    order: 'price ASC',
    limit: 3
}, function(err, products) {
    ...
});</code></pre>
              </div>
              <div class="_col -col-1-2">
                <h3 class="c-heading -h4">iOS Objective-C</h3>
                <pre><code class="language-clike">[MyProduct createWithDictionary:@{
    @"name": @"Pencil",
    @"price": @0.99
}]</code></pre>
              </div>
            </div>
            <div class="c-grid">
              <div class="_col -col-1-2"><br/>
                <h3 class="c-heading -h4">Android Java</h3>
                <pre><code class="language-java">Product product = new Product(
  ImmutableMap.of("name", "Pencil"));

product.save(new Model.Callback() {
  @Override
  public void onSuccess() {
    // saved!
  }
  @Override
  public void onError(Throwable t) {
    // handle errors
  }
});</code></pre>
              </div>
              <div class="_col -col-1-2">
                <h3 class="c-heading -h4">Angular JS</h3>
                <pre><code class="language-javascript">$scope.products = MyProduct.find({
  filter: {
    where: {price: {lt: 100}},
    order: 'price ASC',
    limit: 3
  }
});</code></pre>
              </div>
              <div class="_col -col-1-2"><br/>
                <h3 class="c-heading -h4">Isomorphic LoopBack</h3>
                <pre><code class="language-javascript">MyProduct.find({
  where: {price: {lt: 100}},
  order: 'price ASC',
  limit: 3
}, function(err, products) {
  console.log(products); // => Array of MyProduct
});</code></pre>
              </div>
            </div>
          </div>
        </section>
        <section class="c-section">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Connect to Oracle Database</h2>
            <div class="c-grid">
              <div class="_col -col-1-3">
                <p>Create new datasource:</p>
              </div>
              <div class="_col -col-2-3">
                <pre class="cli"><span class="prompt">$</span> lb datasource
<span class="prompt">?</span> Enter the data-source name: <span class="input">oracledb</span>
<span class="prompt">?</span> Select the connector for oracledb: <span class="input">Oracle (supported by StrongLoop)</span>
<span class="prompt">?</span> Connection String tns <span class="input">[Press Enter]</span>
<span class="prompt">?</span> host: <span class="input">demo.strongloop.com</span>
<span class="prompt">?</span> port: <span class="input">1521</span>
<span class="prompt">?</span> user: <span class="input">Sample</span>
<span class="prompt">?</span> password: <span class="input">demo</span>
<span class="prompt">?</span> database: <span class="input">secret</span>
<span class="prompt">?</span> Install loopback-connector-oracle@^3.0 <span class="input">Yes</span></pre>
              <p>The CLI will automatically run npm to install the LoopBack Oracle connector.
              Now your app can connect to the specified Oracle database.</p></div>
            </div>
<br/>

          </div>
        </section>
        <section class="c-section x-greylight">
          <div class="c-container -padded">
            <h2 class="c-heading -h3">Automatically Discover and Expose Database Tables</h2>
            <div class="c-grid">
              <div class="_col -col-1-3">
                <p>Connectors for Oracle, MySQL, PostgreSQL, and SQL Server provide APIs that enable you to "discover" model definition information from existing databases, including schema information such as column names and datatypes and relation information such as primary and foreign keys.</p>
                <p>Using <a href="https://developer.ibm.com/apiconnect/">IBM API Designer</a> you can accomplish the same thing without any coding!</p>
              </div>
              <div class="_col -col-2-3">
                <pre><code class="language-javascript">var ds = require('../data-sources/db.js')('oracle');
// Discover and build models from INVENTORY table
ds.discoverAndBuildModels('INVENTORY',
    {visited: {}, owner: 'LOOPBACK', associations: true},
    function (err, models) {
        models.Inventory.findOne({}, function (err, inv) {
            console.log("Inventory: ", inv);
            inv.product(function (err, prod) {
                console.log("Product: ", prod);
            });
        });
    }
)</code></pre>
              </div>
            </div>
          </div>
        </section>
      </main>
